package edu.dayu.controller;

import com.github.pagehelper.PageInfo;
import edu.dayu.pojo.Paging;
import edu.dayu.pojo.Tag;
import edu.dayu.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class TagController {
    @Autowired
    private TagService tagService;

    @GetMapping("/blogstag")
    public String toBlogTag(Paging paging, Model model){
        PageInfo<Tag> tagPageInfo = tagService.listTag(paging);
        model.addAttribute("tags",tagPageInfo);
        return "admin/blogs-tags";
    }

    @PostMapping("getTag")
    public String  getTag(Tag tag,Model model) {
        Tag tag1 = tagService.getTagName(tag);
        model.addAttribute("singletag",tag1);
        PageInfo<Tag> tagPageInfo = tagService.listTag(new Paging());
        model.addAttribute("tags",tagPageInfo);
        return "admin/blogs-tags";
    }

    @PostMapping("saveTag")
    @ResponseBody
    public Boolean saveTag(Tag tag) {
        Boolean saveTag = tagService.saveTag(tag);
        return saveTag;
    }

    @PostMapping("updateTag")
    @ResponseBody
    public Boolean updateTag(Tag tag) {
        return tagService.updateTag(tag);
    }

    @PostMapping("deleteTag")
    @ResponseBody
    public void deleteTag(int id) {
        tagService.deleteTag(id);
    }
}
